home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / man / cat.1 / perlovl.1 < prev    next >
Text File  |  1995-07-25  |  21KB  |  463 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLOOOOVVVVLLLL((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLOOOOVVVVLLLL((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.           perlovl - perl overloading semantics
  10.  
  11.      SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.               package SomeThing;
  13.  
  14.               %OVERLOAD = (
  15.                   '+' => \&myadd,
  16.                   '-' => \&mysub,
  17.                   # etc
  18.               );
  19.               ...
  20.  
  21.               package main;
  22.               $a = new SomeThing 57;
  23.               $b=5+$a;
  24.  
  25.  
  26.      CCCCAAAAVVVVEEEEAAAATTTT SSSSCCCCRRRRIIIIPPPPTTTTOOOORRRR
  27.           Overloading of operators is a subject not to be taken
  28.           lightly.  Neither its precise implementation, syntax, nor
  29.           semantics are 100% endorsed by Larry Wall.  So any of these
  30.           may be changed at some point in the future.
  31.  
  32.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  33.           DDDDeeeeccccllllaaaarrrraaaattttiiiioooonnnn ooooffff oooovvvveeeerrrrllllooooaaaaddddeeeedddd ffffuuuunnnnccccttttiiiioooonnnnssss
  34.  
  35.               package Number;
  36.               %OVERLOAD = (
  37.                   "+" => \&add,
  38.                   "*=" => "muas"
  39.               );
  40.  
  41.           declares function _N_u_m_b_e_r::_a_d_d() for addition, and method
  42.           _m_u_a_s() in the "class" Number (or one of its base classes)
  43.           for the assignment form *= of multiplication.  Legal values
  44.           of this hash array are values legal inside &{ ... } call, so
  45.           the name of a subroutine, a reference to a subroutine, or an
  46.           anonymous subroutine will all work.
  47.  
  48.           The subroutine $OVERLOAD{"+"} will be called to execute
  49.           $a+$b if $a is a reference to an object blessed into the
  50.           package Number, or $a is not an object from a package with
  51.           defined mathemagic addition, but $b is a reference to a
  52.           Number.  It can be called also in other situations, like
  53.           $a+=7, or $a++.  See the section on _M_A_G_I_C _A_U_T_O_G_E_N_E_R_A_T_I_O_N.
  54.           (Mathemagical methods refer to methods triggered by an
  55.           overloaded mathematical operator.)
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.      Page 1                                          (printed 6/30/95)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLOOOOVVVVLLLL((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLOOOOVVVVLLLL((((1111))))
  71.  
  72.  
  73.  
  74.           CCCCaaaalllllllliiiinnnngggg CCCCoooonnnnvvvveeeennnnttttiiiioooonnnnssss ffffoooorrrr BBBBiiiinnnnaaaarrrryyyy OOOOppppeeeerrrraaaattttiiiioooonnnnssss
  75.  
  76.           The functions in values %OVERLOAD are called with three (in
  77.           one particular case with four, see the section on _L_a_s_t
  78.           _R_e_s_o_r_t) arguments.  If the corresponding operation is
  79.           binary, then the first two arguments are the two arguments
  80.           of the operation.  However, due to general object calling
  81.           conventions, the first argument should be always an object
  82.           in the package, so in the situation of 7+$a, the order of
  83.           arguments is interchanged.  Most probably it does not matter
  84.           for implementation of the addition method, but whether the
  85.           arguments are reversed is vital for the subtraction method.
  86.           The subroutine can query this information by examining the
  87.           third argument, which can take three different values:
  88.  
  89.           FALSE  the order of arguments is as in the current
  90.                  operation.
  91.  
  92.           TRUE   the arguments are reversed.
  93.  
  94.           undef  the current operation is an assignment variant (as in
  95.                  $a+=7), but the usual function is called instead.
  96.                  This additional information can be used to generate
  97.                  some optimizations.
  98.  
  99.           CCCCaaaalllllllliiiinnnngggg CCCCoooonnnnvvvveeeennnnttttiiiioooonnnnssss ffffoooorrrr UUUUnnnnaaaarrrryyyy OOOOppppeeeerrrraaaattttiiiioooonnnnssss
  100.  
  101.           Unary operation are considered binary operations with the
  102.           second argument being undef.  Thus $OVERLOAD{"++"} is called
  103.           with arguments ($a,undef,'') when $a++ is executed.
  104.  
  105.           OOOOvvvveeeerrrrllllooooaaaaddddaaaabbbblllleeee OOOOppppeeeerrrraaaattttiiiioooonnnnssss
  106.  
  107.           The following keys of %OVERLOAD are recognized:
  108.  
  109.           o+ _A_r_i_t_h_m_e_t_i_c _o_p_e_r_a_t_i_o_n_s
  110.  
  111.                    "+", "+=", "-", "-=", "*", "*=", "/", "/=", "%", "%=",
  112.                    "**", "**=", "<<", "<<=", ">>", ">>=", "x", "x=", ".", ".=",
  113.  
  114.                For these operations a substituted non-assignment
  115.                variant can be called if the assignment variant is not
  116.                available.  Methods for operations "+", "-", "+=", and
  117.                "-=" can be called to automatically generate increment
  118.                and decrement methods.  The operations "-" can be used
  119.                to autogenerate missing methods for unary minus or abs.
  120.  
  121.           o+ _C_o_m_p_a_r_i_s_o_n _o_p_e_r_a_t_i_o_n_s
  122.  
  123.                    "<",  "<=", ">",  ">=", "==", "!=", "<=>",
  124.                    "lt", "le", "gt", "ge", "eq", "ne", "cmp",
  125.  
  126.  
  127.  
  128.  
  129.      Page 2                                          (printed 6/30/95)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLOOOOVVVVLLLL((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLOOOOVVVVLLLL((((1111))))
  137.  
  138.  
  139.  
  140.                If the corresponding "spaceship" variant is available,
  141.                it can be used to substitute for the missing operation.
  142.                During sorting arrays, cmp is used to compare values
  143.                subject to %OVERLOAD.
  144.  
  145.           o+ _B_i_t _o_p_e_r_a_t_i_o_n_s
  146.  
  147.                    "&", "^", "|", "neg", "!", "~",
  148.  
  149.                "neg" stands for unary minus.  If the method for neg is
  150.                not specified, it can be autogenerated using on the
  151.                method for subtraction.
  152.  
  153.           o+ _I_n_c_r_e_m_e_n_t _a_n_d _d_e_c_r_e_m_e_n_t
  154.  
  155.                    "++", "--",
  156.  
  157.                If undefined, addition and subtraction methods can be
  158.                used instead.  These operations are called both in
  159.                prefix and postfix form.
  160.  
  161.           o+ _T_r_a_n_s_c_e_n_d_e_n_t_a_l _f_u_n_c_t_i_o_n_s
  162.  
  163.                    "atan2", "cos", "sin", "exp", "abs", "log", "sqrt",
  164.  
  165.                If abs is unavailable, it can be autogenerated using
  166.                methods for "<" or "<=>" combined with either unary
  167.                minus or subtraction.
  168.  
  169.           o+ _B_o_o_l_e_a_n, _s_t_r_i_n_g _a_n_d _n_u_m_e_r_i_c _c_o_n_v_e_r_s_i_o_n
  170.  
  171.                    "bool", "\"\"", "0+",
  172.  
  173.                If one or two of these operations are unavailable, the
  174.                remaining ones can be used instead.  bool is used in
  175.                the flow control operators (like while) and for the
  176.                ternary "?:" operation.  These functions can return any
  177.                arbitrary Perl value.  If the corresponding operation
  178.                for this value is overloaded too, that operation will
  179.                be called again with this value.
  180.  
  181.           o+ _S_p_e_c_i_a_l
  182.  
  183.                    "nomethod", "fallback", "=",
  184.  
  185.                see the section on _S_P_E_C_I_A_L _K_E_Y_S _O_F %_O_V_E_R_L_O_A_D.
  186.  
  187.           See the section on _F_a_l_l_b_a_c_k for an explanation of when a
  188.           missing method can be autogenerated.
  189.  
  190.      SSSSPPPPEEEECCCCIIIIAAAALLLL KKKKEEEEYYYYSSSS OOOOFFFF %%%%OOOOVVVVEEEERRRRLLLLOOOOAAAADDDD
  191.           Three keys are recognized by Perl that are not covered by
  192.  
  193.  
  194.  
  195.      Page 3                                          (printed 6/30/95)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLOOOOVVVVLLLL((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLOOOOVVVVLLLL((((1111))))
  203.  
  204.  
  205.  
  206.           the above description.
  207.  
  208.           LLLLaaaasssstttt RRRReeeessssoooorrrrtttt
  209.  
  210.           $OVERLOAD{"nomethod"} is a reference to a function of four
  211.           parameters.  If defined, it is called when the overloading
  212.           mechanism cannot find a method for some operation.  The
  213.           first three arguments of this function coincide with
  214.           arguments for the corresponding method if it were found, the
  215.           fourth argument is the key of %OVERLOAD corresponding to the
  216.           missing method.  If several methods are tried, the last one
  217.           is used.  Say, 1-$a can be equivalent to
  218.  
  219.                   &{ $Pack::OVERLOAD{"nomethod"} }($a,1,1,"-").
  220.  
  221.           If some operation cannot be resolved, and there is no
  222.           $OVERLOAD{"nomethod"}, then an exception will be raised via
  223.           _d_i_e() -- unless $OVERLOAD{"fallback"} is true.
  224.  
  225.           FFFFaaaallllllllbbbbaaaacccckkkk
  226.  
  227.           $OVERLOAD{"fallback"} governs what to do if a method for a
  228.           particular operation is not found.  Three different cases
  229.           are possible depending on value of $OVERLOAD{"fallback"}:
  230.  
  231.           o+ undef         Perl tries to use a substituted method (see
  232.                           the section on _M_A_G_I_C _A_U_T_O_G_E_N_E_R_A_T_I_O_N).  If
  233.                           this fails, it then tries to calls
  234.                           $OVERLOAD{"nomethod"}; if missing, an
  235.                           exception will be raised.
  236.  
  237.           o+ TRUE          The same as for the undef value, but no
  238.                           exception is raised.  Instead, it silently
  239.                           reverts to what it would have done were
  240.                           there no %OVERLOAD is present.
  241.  
  242.           o+ defined, but FALSE
  243.                           No autogeneration is tried.  Perl tries to
  244.                           call $OVERLOAD{"nomethod"}, and if this is
  245.                           missing, raises an exception.
  246.  
  247.           CCCCooooppppyyyy CCCCoooonnnnssssttttrrrruuuuccccttttoooorrrr
  248.  
  249.           $OVERLOAD{"="} is a reference to a function with three
  250.           arguments, i.e., it looks like a usual value of %OVERLOAD.
  251.           What is special about this subroutine is that it should not
  252.           return a blessed reference into a package (as most other
  253.           methods are expected to), but rather a freshly made copy of
  254.           its dereferenced argument (see the section on _B_U_G_S, though).
  255.           This operation is called in the situations when a mutator is
  256.           applied to a reference that shares its object with some
  257.           other reference, such as
  258.  
  259.  
  260.  
  261.      Page 4                                          (printed 6/30/95)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLOOOOVVVVLLLL((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLOOOOVVVVLLLL((((1111))))
  269.  
  270.  
  271.  
  272.                   $a=$b;
  273.                   $a++;
  274.  
  275.           To make this change to $a and not to change $b, a freshly
  276.           made copy of $$a is made, and $a is assigned a reference to
  277.           this object.  This operation is executed during $a++, (so
  278.           before this $$a coincides with $$b), and only if ++ is
  279.           expressed via  $OPERATOR{'++'} or $OPERATOR{'+='}.  Note
  280.           that if this operation is expressed via '+', i.e., as
  281.  
  282.                   $a=$b;
  283.                   $a=$a+1;
  284.  
  285.           then $$a and $$b do not appear as lvalues.
  286.  
  287.           If the copy constructor is required during execution of some
  288.           mutator, but $OPERATOR{'='} is missing, it can be
  289.           autogenerated as a string copy if an object of the package
  290.           is a plain scalar.
  291.  
  292.      MMMMAAAAGGGGIIIICCCC AAAAUUUUTTTTOOOOGGGGEEEENNNNEEEERRRRAAAATTTTIIIIOOOONNNN
  293.           If a method for an operation is not found, and
  294.           $OVERLOAD{"fallback"} is TRUE or undefined, Perl tries to to
  295.           autogenerate a substitute method for the missing operation
  296.           based on defined operations.  Autogenerated method
  297.           substitutions are possible for the following operations:
  298.  
  299.           _A_s_s_i_g_n_m_e_n_t _f_o_r_m_s _o_f _a_r_i_t_h_m_e_t_i_c _o_p_e_r_a_t_i_o_n_s
  300.                           $a=+$b can use the $OVERLOAD{"+"} method if
  301.                           $OVERLOAD{"+="} is not defined.
  302.  
  303.           _C_o_n_v_e_r_s_i_o_n _o_p_e_r_a_t_i_o_n_s
  304.                           String, numeric, and boolean conversion are
  305.                           calculated in terms of one another if not
  306.                           all of them are defined.
  307.  
  308.           _I_n_c_r_e_m_e_n_t _a_n_d _d_e_c_r_e_m_e_n_t
  309.                           The ++$a operation can be expressed in terms
  310.                           of $a+=1 or $a+1, and $a-- in terms of $a-=1
  311.                           and $a-1.
  312.  
  313.           abs($a)         can be expressed in terms of $a<0 and -$a
  314.                           (or 0-$a).
  315.  
  316.           _U_n_a_r_y _m_i_n_u_s     can be expressed in terms of subtraction.
  317.  
  318.           _C_o_n_c_a_t_e_n_a_t_i_o_n   can be expressed in terms of string
  319.                           conversion.
  320.  
  321.           _C_o_m_p_a_r_i_s_o_n _o_p_e_r_a_t_i_o_n_s
  322.                           can be expressed in terms of its "spaceship"
  323.                           counterpart: either <=> or cmp:
  324.  
  325.  
  326.  
  327.      Page 5                                          (printed 6/30/95)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLOOOOVVVVLLLL((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLOOOOVVVVLLLL((((1111))))
  335.  
  336.  
  337.  
  338.                               <, >, <=, >=, ==, !=      in terms of
  339.                           <=>
  340.                               lt, gt, le, ge, eq, ne    in terms of
  341.                           cmp
  342.  
  343.           _C_o_p_y _o_p_e_r_a_t_o_r   can be expressed in terms of assignment to
  344.                           the dereferenced value, if this value is
  345.                           scalar but not a reference.
  346.  
  347.      WWWWAAAARRRRNNNNIIIINNNNGGGG
  348.           The restriction for the comparison operation is that even
  349.           if, for example, `cmp' should return a blessed reference,
  350.           the autogenerated `lt' function will produce only a standard
  351.           logical value based on the numerical value of the result of
  352.           `cmp'.  In particular, a working numeric conversion is
  353.           needed in this case (possibly expressed in terms of other
  354.           conversions).
  355.  
  356.           Similarly, .=  and x= operators lose their mathemagical
  357.           properties if the string conversion substitution is applied.
  358.  
  359.           When you _c_h_o_p() a mathemagical object, it becomes promoted
  360.           to a string first, and its mathemagical qualities is lost.
  361.           The same can happen with other operations as well.
  362.  
  363.      IIIIMMMMPPPPLLLLEEEEMMMMEEEENNNNTTTTAAAATTTTIIIIOOOONNNN
  364.           The table of methods for all operations is cached as a magic
  365.           for the symbol table hash of the package.  It is rechecked
  366.           for changes of %OVERLOAD and @ISA only during blessing; so
  367.           if it is changed dynamically, you'll need an additional fake
  368.           blessing to update the table.
  369.  
  370.           (Every SVish thing has a magic queue, and a magic is an
  371.           entry in that queue.  This is how a single variable may
  372.           participate in multiple forms of magic simultaneously.  For
  373.           instance, environment variables regularly have two forms at
  374.           once: their %ENV magic and their taint magic.)
  375.  
  376.           If an object belongs to a package with %OVERLOAD, it carries
  377.           a special flag.  Thus the only speed penalty during
  378.           arithmetic operations without overload is the check of this
  379.           flag.
  380.  
  381.           In fact, if no %OVERLOAD is ever accessed, there is almost
  382.           no overhead for overloadable operations, so most programs
  383.           should not suffer measurable performance penalties.
  384.           Considerable effort was made minimize overhead when
  385.           %OVERLOAD is accessed and the current operation is
  386.           overloadable but the arguments in question do not belong to
  387.           packages with %OVERLOAD.  When in doubt, test your speed
  388.           with %OVERLOAD and without it.  So far there have been no
  389.           reports of substantial speed degradation if Perl is compiled
  390.  
  391.  
  392.  
  393.      Page 6                                          (printed 6/30/95)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      PPPPEEEERRRRLLLLOOOOVVVVLLLL((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLOOOOVVVVLLLL((((1111))))
  401.  
  402.  
  403.  
  404.           with optimization turned on.
  405.  
  406.           There is no size penalty for data if there is no %OVERLOAD.
  407.  
  408.           The copying like $a=$b is shallow; however, a one-level-deep
  409.           copying is carried out before any operation that can imply
  410.           an assignment to the object $b (or $a) refers to, like $b++.
  411.           You can override this behavior by defining your copy
  412.           constructor (see the section on _C_o_p_y _C_o_n_s_t_r_u_c_t_o_r).
  413.  
  414.           It is expected that arguments to methods that are not
  415.           explicitly supposed to be changed are constant (but this is
  416.           not enforced).
  417.  
  418.      AAAAUUUUTTTTHHHHOOOORRRR
  419.           Ilya Zakharevich <_i_l_y_a@_m_a_t_h._m_p_s._o_h_i_o-_s_t_a_t_e._e_d_u>.
  420.  
  421.      DDDDIIIIAAAAGGGGNNNNOOOOSSSSTTTTIIIICCCCSSSS
  422.           When Perl is run with the ----DDDDoooo switch or its equivalent,
  423.           overloading induces diagnostic messages.
  424.  
  425.      BBBBUUUUGGGGSSSS
  426.           Because it's used for overloading, the per-package
  427.           associative array %OVERLOAD now has a special meaning in
  428.           Perl.
  429.  
  430.           Although the copy constructor is specially designed to make
  431.           overloading operations with references to an array simpler,
  432.           as it now works it's useless for this because a subroutine
  433.           cannot return an array in the same way as it returns a
  434.           scalar (from the point of view of Perl internals).  Expect a
  435.           change of interface for the copy constructor.
  436.  
  437.           As shipped, %OVERLOAD is not inherited via the @ISA tree.  A
  438.           patch for this is available from the author.
  439.  
  440.           This document is confusing.
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.      Page 7                                          (printed 6/30/95)
  460.  
  461.  
  462.  
  463.